package com.ssy.lingxi.common.handler;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author ds
 * @version 2.0.0
 * @date 2021/4/23
 */
public class Double2Serialize extends JsonSerializer<Double> {

    @Override
    public void serialize(Double data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        String bigDecimalStr = "0";
        if (data != null&&!data.equals(0D)) {
            bigDecimalStr=BigDecimal.valueOf(data).setScale(2, RoundingMode.HALF_UP).toPlainString();
        }
        try {
            jsonGenerator.writeString(bigDecimalStr);
        } catch (IOException e) {
        }
    }

}
